﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;
using Cosmetic.DataAccess;
using Cosmetic.DataEntity;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace Cosmetic.Logic
{
    /// <summary>
    /// Category manager class
    /// </summary>
    public class NewClassManger : LogicBase
    {
        /// <summary>
        /// top level category
        /// </summary>
        public enum ETopCategory
        {
            /// <summary>
            /// Product category. ie: 美白, 防晒...
            /// </summary>
            Product,

            /// <summary>
            /// Product category. ie: 美白, 防晒...
            /// </summary>
            Category_L,

            /// <summary>
            /// ie:欧美品牌，日本品牌，韩国品牌，其他...
            /// </summary>
            BrandArea,
            /// <summary>
            /// forum board categroy. ie: 脸部保养, 彩妆...
            /// </summary>
            Forum,
            /// <summary>
            /// topic state. ie: Top, Close ...
            /// </summary>
            TopicState,
            /// <summary>
            /// content category. ie: 最新情报, 美丽宝典...
            /// </summary>
            Content,
            /// <summary>
            /// tag attached to which category. ie: Comment, Content
            /// </summary>
            Tag,
            /// <summary>
            /// user role. ie: Admin, Blog, ForumAdmin
            /// </summary>
            Role,
            /// <summary>
            /// user skin kind. ie: 干性, 油性...
            /// </summary>
            UserSkin,
            /// <summary>
            /// user hair kind. ie: 中性, 受损...
            /// </summary>
            UserHair,
            /// <summary>
            /// job kind. ie: 工程师, 营业员...
            /// </summary>
            Job,
            /// <summary>
            /// cost range. ie: 0--100, 100-200...
            /// </summary>
            Cost,
            /// <summary>
            /// education. ie: 小学, 中学, 大学...
            /// </summary>
            Education,
            /// <summary>
            /// 购买渠道. ie: 专柜, 超市 ....
            /// </summary>
            PurchaseChannels,
            /// <summary>
            /// 博客的类别
            /// </summary>
            Blog,
            /// <summary>
            /// 博客文章的类别
            /// </summary>
            Topic
        }

        /// <summary>
        /// 判断是否有类别，如果有返回true，否则返回false.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool IsExistTheName(string name)
        {
            var ex = (from category in m_CosmeticDC.newclass 
                      where category.Name  == name
                      select category).ToList();
            if (ex != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// Insert a new Category into database
        /// </summary>
        /// <param name="aCategory">new Category</param>
        public void Insert(newclass  aCategory)
        {
            if (aCategory.Index == 0)
            {
                aCategory.Index = m_CosmeticDC.newclass .Max(theCategory => theCategory.Index) + 1;
            }
            m_CosmeticDC.newclass.InsertOnSubmit(aCategory);
            m_CosmeticDC.SubmitChanges();
        }

        /// <summary>
        /// Delete Category
        /// </summary>
        /// <param name="aCategory">The Category want delete</param>
        public void Delete(newclass  aCategory)
        {
            m_CosmeticDC.newclass.DeleteOnSubmit(aCategory);
            m_CosmeticDC.SubmitChanges();
        }





        /// <summary>
        /// check top level category whether or not exist, if not exist then creat it.
        /// </summary>
        public void CheckTopCategory()
        {
            Type theTopCategoryType = typeof(ETopCategory);
            foreach (string theTopCategoryName in Enum.GetNames(theTopCategoryType))
            {
                ETopCategory theTopCategoy = (ETopCategory)Enum.Parse(typeof(ETopCategory), theTopCategoryName);
                if (GetTopCategryByName(theTopCategoy) == null)
                {
                    newclass  theTopCategory = new newclass ();
                    theTopCategory.Name = theTopCategoryName;
                    theTopCategory.CategoryId = Guid.NewGuid();
                    theTopCategory.SupCategoryId = theTopCategory.CategoryId;
                    theTopCategory.CreateTime = DateTime.Now;
                    Insert(theTopCategory);
                }
            }
        }

        /// <summary>
        /// check the supcategory whether or not contains subcategory.
        /// </summary>
        /// <param name="aSupCategory">supcategory</param>
        /// <param name="aSubCategoryName">subcategory name</param>
        /// <returns>if contians return true else return false</returns>
        public bool CheckContains(newclass  aSupCategory, string aSubCategoryName)
        {
            return this.CheckContains(aSupCategory.CategoryId, aSubCategoryName);
        }
        /// <summary>
        /// check the supcategory whether or not contains subcategory.
        /// </summary>
        /// <param name="aSupCategoryId">supcategory id</param>
        /// <param name="aSubCategoryName">subcategory name</param>
        /// <returns>if contians return true else return false</returns>
        public bool CheckContains(Guid aSupCategoryId, string aSubCategoryName)
        {
            List<newclass > theCategoryList = (from theCategory in m_CosmeticDC.newclass 
                                              where theCategory.SupCategoryId == aSupCategoryId && theCategory.Name == aSubCategoryName
                                              select theCategory).ToList();
            return theCategoryList.Count > 0;
        }

        /// <summary>
        /// Get Category by Category Id
        /// </summary>
        /// <param name="aCategoryId">Category Id</param>
        /// <returns>Category</returns>
        public newclass  GetCategoryById(Guid aCategoryId)
        {
            try
            {
                return m_CosmeticDC.newclass .Single(theCategory => theCategory.CategoryId == aCategoryId);
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Get Category by Category Id
        /// </summary>
        /// <param name="aCategoryId">Category Id</param>
        /// <returns>Category</returns>
        public string GetNameByCategoryId(Guid aCategoryId)
        {
            var name = (from cate in m_CosmeticDC.newclass 
                        where cate.CategoryId == aCategoryId
                        select cate.Name
                          ).Single();
            return name;
        }


        public List<newclass > GetUpCategoryBySupCategoryID(Guid aCategoryID)
        {
            var aa = (from cate in m_CosmeticDC.newclass 
                      where cate.SupCategoryId == aCategoryID
                      select cate).ToList();
            return aa;
        }

        /// <summary>
        /// 得到这一级的Category类。
        /// </summary>
        /// <param name="aCategoryID"></param>
        /// <returns></returns>
        public newclass  GetCategoryByCategoryID(Guid aCategoryID)
        {
            var aa = (from cate in m_CosmeticDC.newclass
                      where cate.CategoryId == aCategoryID
                      select cate).Single();
            return aa;
        }

        public List<newclass > GetUpCategoryById(Guid aCategoryID)
        {
            var aa = (from cate in m_CosmeticDC.newclass 
                      where cate.SupCategoryId == (from category in m_CosmeticDC.newclass 
                                                   where category.CategoryId == aCategoryID
                                                   select category.SupCategoryId).Single()
                      orderby cate.CreateTime descending
                      select cate).ToList();
            return aa;
        }

        /// <summary>
        /// 用来得到上一级的CategoryId.
        /// </summary>
        /// <param name="aCategoryID"></param>
        /// <returns></returns>
        public newclass  GetFrontCategoryById(Guid aCategoryID)
        {
            var aa = (from cate in m_CosmeticDC.newclass 
                      where cate.CategoryId == (from category in m_CosmeticDC.newclass 
                                                where category.CategoryId == aCategoryID
                                                select category.SupCategoryId).Single()
                      select cate).Single();
            return aa;
        }

        public List<newclass > GetCategoryById_L(Guid aCategoryID)
        {
            var aa = (from cate in m_CosmeticDC.newclass 
                      where cate.SupCategoryId == aCategoryID
                      orderby cate.CreateTime descending
                      select cate).ToList();
            return aa;
        }

        public string GetCategoryByID(string str)
        {
            try
            {
                var strTem = (from cate in m_CosmeticDC.newclass 
                              where cate.Name == str
                              select cate.CategoryId).Single().ToString();
                return strTem;
            }
            catch
            { return ""; }
        }

        public string GetCategoryIdByName(string CategoryName)
        {
            var result = from cate in m_CosmeticDC.newclass 
                         where cate.Name == CategoryName
                         select cate;
            return result.First().CategoryId.ToString();
        }

        /// <summary>
        /// Get Category by Category Name
        /// </summary>
        /// <param name="aCategoryId">Category Name</param>
        /// <returns>Category</returns>
        public newclass  GetCategoryByName(string aCategoryName)
        {
            try
            {
                return m_CosmeticDC.newclass .First(theCategory => theCategory.Name == aCategoryName);
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// get subcatgories by supcategory
        /// </summary>
        /// <param name="aSupCategory">supcategory</param>
        /// <returns>subcategories</returns>
        public List<newclass > GetSubCateogoriesBySupCategory(newclass  aSupCategory)
        {
            try
            {
                return GetSubCateogoriesBySupCategory(aSupCategory.CategoryId);
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// get subcatgories by supcategory id
        /// </summary>
        /// <param name="aSupCategoryId">supcategory id</param>
        /// <returns>subcategories</returns>
        public List<newclass > GetSubCateogoriesBySupCategory(Guid aSupCategoryId)
        {
            var result = (from theCategory in m_CosmeticDC.newclass 
                          where theCategory.SupCategoryId == aSupCategoryId && theCategory.CategoryId != theCategory.SupCategoryId
                          orderby theCategory.CreateTime descending
                          select theCategory).ToList();
            return result;
        }

        /// <summary>
        /// get top level category by category name, if not find the category create it
        /// </summary>
        /// <param name="aTopCategory">top level category</param>
        /// <returns>top level category</returns>
        public newclass  GetTopCategryByName(ETopCategory aTopCategory)
        {
            List<newclass > theCategoryList = (from theCategory in m_CosmeticDC.newclass 
                                              where theCategory.Name == aTopCategory.ToString() && theCategory.CategoryId == theCategory.SupCategoryId
                                              select theCategory).ToList();
            if (theCategoryList.Count == 0)
            {
                newclass  theCategory = new newclass ();
                theCategory.Name = aTopCategory.ToString();
                theCategory.CategoryId = Guid.NewGuid();
                theCategory.CreateTime = DateTime.Now;
                theCategory.SupCategoryId = theCategory.CategoryId;
                this.Insert(theCategory);
                return this.GetTopCategryByName(aTopCategory);
            }
            return theCategoryList[0];
        }


        public static DataTable GetProductInfoByIds(Guid aProductId)
        {
            string conStr = ConfigurationManager.ConnectionStrings["CosmeticConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(conStr);

            try
            {
                con.Open();
                string cmdStr = string.Empty;
                cmdStr = "select Name,CreateTime from newclass where CategoryId='" + aProductId + "'";
                SqlCommand com = new SqlCommand(cmdStr, con);
                SqlDataAdapter adap = new SqlDataAdapter(com);
                DataSet ds = new DataSet();
                adap.Fill(ds);
                return ds.Tables[0];
            }
            finally
            {
                con.Close();
            }
        }

        /// <summary>
        /// get second level category by category name, if not find the category create it
        /// </summary>
        /// <param name="aCategoryName">second level name</param>
        /// <param name="aTopCategory">top level category</param>
        /// <returns>second level category</returns>
        public newclass  GetSecondCategoryByName(String aCategoryName, ETopCategory aTopCategory)
        {
            newclass  theTopCategory = GetTopCategryByName(aTopCategory);

            List<newclass> theCategoryList = (from theCategory in m_CosmeticDC.newclass 
                                              where theCategory.Name == aCategoryName && theTopCategory.CategoryId == theCategory.SupCategoryId
                                              select theCategory).ToList();
            if (theCategoryList.Count == 0)
            {
                newclass  theCategory = new newclass ();
                theCategory.Name = aCategoryName;
                theCategory.CategoryId = Guid.NewGuid();
                theCategory.CreateTime = DateTime.Now;
                theCategory.SupCategoryId = theTopCategory.CategoryId;
                this.Insert(theCategory);
                return this.GetSecondCategoryByName(aCategoryName, aTopCategory);
            }
            return theCategoryList[0];
        }

        /// <summary>
        /// get all top level category
        /// </summary>
        /// <returns>all top level category list</returns>
        public List<newclass > GetAllTopCategory()
        {
            return (from theCategory in m_CosmeticDC.newclass 
                    where theCategory.CategoryId == theCategory.SupCategoryId
                    orderby theCategory.CreateTime descending
                    select theCategory).ToList();
        }

        //public Dictionary<String, Guid> GetBlogCategory(newclass  aCategory)
        //{
        //    newclass theTopCategory = GetTopCategryByName(NewClassManger.ETopCategory.Topic);

        //    List<newclass> theCategoryList = GetSubCateogoriesBySupCategory(theTopCategory.CategoryId);
        //    Dictionary<String, Guid> theCategories = new Dictionary<String, Guid>();
        //    if (theCategoryList.Count() > 0)
        //    {
        //        IOrderedQueryable<Blog> theBlogList = m_CosmeticDC.Blogs.Where(theBlog => theBlog.BlogCategory == aCategory).OrderBy(theBlog => theBlog.BlogCategoryId);

        //        foreach (newclass theCategory in theCategoryList)
        //        {
        //            String theKey = String.Format("{0} ({1}篇)",
        //                                theCategory.Name,
        //                                theBlogList.Count(theBlog => theBlog.CategoryId == theCategory.CategoryId));
        //            theCategories.Add(theKey, theCategory.CategoryId);
        //        }
        //    }
        //    return theCategories;
        //}
    }
}

